package jhelp;

import java.awt.*;
import javax.swing.*;

public class Client extends JFrame implements View, Observer {
	
	JTextField textField = new JTextField();
	JTextArea textArea = new JTextArea();
	JLabel termLabel = new JLabel("Term:");
	JLabel defLabel = new JLabel("Definitions");
	JButton bFind = new JButton("Find");
	JButton bAdd = new JButton("Add");
	JButton bEdit = new JButton("Edit");
	JButton bDelete = new JButton("Delete");
	JButton bNext = new JButton("Next");
	JButton bPrevous = new JButton("Prevous");
	JButton bExit = new JButton("Exit");
	Component fEmpArea = Box.createRigidArea(null);
	Component sEmpArea = Box.createRigidArea(null);
	Container cp = getContentPane();
	JMenuBar menu = new JMenuBar();
	JMenuBar mainMenu = new JMenuBar();
	
	Controller controller;
	Observable model;
	
	
	public Client (Observable model, Controller controller) {
		
		this.controller = controller;
		
		setSize(640,480);
		
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		cp.setLayout(gridbag);
		
				
		JMenu fileMenu = new JMenu("Data source");
		JMenuItem openFileItem = new JMenuItem("TXT");
		JMenuItem openXMLItem = new JMenuItem("XML");
		JMenuItem exitFileItem = new JMenuItem("Exit");
		JMenuItem openDBItem = new JMenuItem("Database");
		fileMenu.add(openFileItem);
		fileMenu.add(openXMLItem);
		fileMenu.add(openDBItem);
		fileMenu.add(exitFileItem);
		mainMenu.add(fileMenu);
		this.setJMenuBar(mainMenu);
		termLabel.setBounds(100, 60, 440, 360);
        termLabel.setEnabled(true);
        defLabel.setBounds(100, 60, 440, 360);
        defLabel.setEnabled(true);
        
        
		setTitle("Demo JHelp Frame");
		setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);		
				
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets (12, 2, 12, 2);
				
		addComponent (termLabel, gridbag, gbc);
		
		gbc.weightx = 8.0;
		addComponent (textField, gridbag, gbc);
		
		gbc.weightx = 0.0;
		gbc.gridheight = 1;
		addComponent (bFind, gridbag, gbc);
		
		gbc.insets = new Insets (2, 2, 2, 2);
		gbc.gridy = 1;
		addComponent (defLabel, gridbag, gbc);
		
		gbc.gridy = 2;
		gbc.weightx = 8.0;
		gbc.gridwidth = 2;
		gbc.gridheight = 8;
		gbc.weighty = 8.0;
		addComponent(textArea, gridbag, gbc);
		
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weighty = 0.0;
		gbc.weightx = 0.0;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		addComponent (bAdd, gridbag, gbc);		
		gbc.gridy = 3;
		addComponent (bEdit, gridbag, gbc);
		gbc.gridy = 4;
		addComponent(bDelete, gridbag, gbc);
		
		
		gbc.gridy = 5;
		gbc.weighty = 0.3;
		addComponent(sEmpArea, gridbag,gbc);
		
		gbc.gridy = 6;
		gbc.weighty = 0.0;
		addComponent(bNext, gridbag, gbc);
		gbc.gridy = 7;
		addComponent(bPrevous, gridbag, gbc);
		
		gbc.gridy = 8;
		gbc.weighty = 3;
		addComponent(fEmpArea, gridbag,gbc);
		
		gbc.gridy = 9;
		gbc.weighty = 0.0;
		addComponent(bExit, gridbag, gbc);
		
		ClientListener cl = new ClientListener(this, controller);
		bAdd.addActionListener(cl);
		bExit.addActionListener(cl);
		bExit.setActionCommand("exit");
		
		textField.addKeyListener(cl);
		
		openFileItem.addActionListener(cl);
		openFileItem.setActionCommand("openfile");
		openXMLItem.setActionCommand("openxml");
		openDBItem.setActionCommand("opendb");
		openXMLItem.addActionListener(cl);
		exitFileItem.addActionListener(cl);
		openDBItem.addActionListener(cl);
		exitFileItem.setActionCommand("exit");
		bFind.setActionCommand("find");
		bFind.addActionListener(cl);
		
		textArea.setWrapStyleWord(true);
		textArea.setLineWrap(true);
		
		setVisible(true);
	}

	/*public static void main (String [] args) {
		Client frame = new Client();
		frame.run();
	}*/
	
	protected void addComponent (Component comp, GridBagLayout gridbag, GridBagConstraints gbc) {
		gridbag.setConstraints(comp, gbc);
		this.getContentPane().add(comp);
	}
	
	public void run() {
        System.out.println("Client: run");
    }

	public String getTextToFind() {
		// TODO Auto-generated method stub
		return textField.getText();
	}

	public void close() {
		this.dispose();
	}

	
	public void update(Model model) {
		textArea.setText(model.getCurrentState().getValue());
		repaint();
	}
	public void setObservable(Observable model){
		this.model = model;
		model.addObserver(this);
	}
	
}
